package com.lzx.mq.Listener.user;

import com.alibaba.fastjson.JSONObject;
import com.lzx.mq.feign.*;
import com.lzx.mq.vo.NicknameVO;
import com.lzx.mq.vo.QueryUser;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 程序员星星
 * @date 2023/2/12
 * @Description
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "user-nickname-group", topic = "nickname-edit-topic")
public class UpdateNicknameListener implements RocketMQListener<String> {
    @Resource
    private UserClient userClient;

    @Resource
    private MakeFriendClient makeFriendClient;

    @Resource
    private ArticleClient articleClient;

    @Resource
    private AnswerClient answerClient;

    @Resource
    private ESClient esClient;

    @GlobalTransactional
    @Override
    public void onMessage(String message) {
        NicknameVO nicknameVO = JSONObject.parseObject(message, NicknameVO.class);
        //修改用户昵称(通知)
        userClient.updateNicknameToInform(nicknameVO);
        //修改用户昵称(好友聊天)
        makeFriendClient.updateNicknameToChat(nicknameVO);
        //修改用户昵称(文章一级评论)
        articleClient.updateNicknameToComments(nicknameVO);
        //修改用户昵称(文章二级评论)
        articleClient.updateNicknameToSecondComments(nicknameVO);
        //修改用户昵称(问题)
        answerClient.updateNicknameToProblem(nicknameVO);
        //修改用户昵称(问题一级回复)
        answerClient.updateNicknameToReply(nicknameVO);
        //修改用户昵称(问题二级回复)
        answerClient.updateNicknameToSecondReply(nicknameVO);
        //es数据同步
        QueryUser queryUser = new QueryUser();
        queryUser.setId(nicknameVO.getId());
        queryUser.setNickname(nicknameVO.getNickname());
        //文章服务同步
        esClient.updateNicknameOrAvatar(queryUser);
        //问答服务同步
        esClient.updateUserNicknameOrAvatar(queryUser);
        log.info("同步成功");
    }
}
